<!DOCTYPE html>
<html th:replace="~{layouts/layout :: layout(~{::title}, ~{::section})}" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title th:text="${article.getTitle()} + ' - 青池'"></title>
</head>
<body>
<section>
    <style>
        #title {
            text-align: center; }

        #date {
            width: 80%;
            margin: 0 auto;
            padding: 10px;
            text-align: center;
            color: #aaa;
            border-bottom: 1px #ccc solid; }

        ul {
            list-style-position: inside; }

        #content {
            width: 90%;
            margin: 0 auto;
            line-height: 220%;
            font-size: 18px; }
        @media screen and (min-width: 800px) {
            #content {
                width: 80%; } }
        #content p {
            margin-bottom: 20px; }

        #stage .tag-list {
            width: 90%;
            margin: 0 auto;
            margin-top: 20px;
            list-style: none;
            background: #eee; }
        @media screen and (min-width: 800px) {
            #stage .tag-list {
                width: 80%; } }
        #stage .tag-list li {
            display: inline-block;
            margin: 10px; }

        #commentFormWrapper {
            width: 90%;
            margin-left: 5%;
            margin-bottom: 15px;
            padding: 15px;
            background: #fafafa;
            border: 1px #aaa solid;
        }
        #commentForm {
            display: inline-block;
            width: 100%;
            padding: 15px;
        }  input {
               height: 30px;
           }
        .input {
            width: 100%;
            border: 1px #ccc solid;
        }
        #submit {
            margin-top: 15px;
            width: 80px;
            height: 35px;
            background: #006699;
            border: 1px #093059 solid;
            color: #fff;
        }
        .commentList {
            display: inline-block;
            width: 100%;
            height: 280px;
            overflow-y: auto;
            float: right;
            padding: 15px;
            background: #fff;
            border:1px #ddd solid;
        }
        .commentList li {
            list-style: none;
            width: 100%;
        }
        @media screen and (min-width: 800px) {
            #commentForm {
                width: 50%;
            }
            .commentList {
                width: 45%;
            }
        }
        .commentWrapper {
            padding: 15px;
            margin-bottom: 5px;
            background: #fafafa;
            border: 1px #ddd solid;
        }
        .contentWrapper {
            padding: 5px;
            margin: 5px 0;
        }
        .blank {
            display: inline-block;
            width: 100%;
            text-align: center;
            font-size: 13px;
            color: #aaa;
            line-height: 230px;
        }
    </style>
    <div id="stage">
        <h2 id="title" th:text="${article.getTitle()}"></h2>
        <br>
        <div id="content" th:utext="${article.getContent()}"></div>
    </div>
    <div id="commentFormWrapper">
        <table id="commentForm">
            <tr>
                <td>昵称：</td>
                <td>
                    <input class="input" type="text" id="username">
                </td>
            </tr>
            <tr>
                <td>内容：</td>
                <td>
                    <textarea class="input" cols="30" rows="10"></textarea>
                </td>
            </tr>
            <tr>
                <td></td>
                <td>
                    <input id="submit" type="submit" value="发表评论">
                </td>
            </tr>
        </table>
        <div class="commentList">
            <ul th:each="comment : ${comments}">
                <li class="commentWrapper">
                    <h5 th:text="${comment.getUsername()+': '}"></h5>
                    <p class="contentWrapper" th:text="${comment.getContent()}"></p>
                </li>
            </ul>
            <span class="blank" th:if="${comments.size()==0}">还没有评论哦...</span>
        </div>
        <div style="clear: both"></div>
    </div>

    <script>
        var submitBtn = document.querySelector('#submit');

        submitBtn.onclick = function() {
          var usernameNode = document.querySelector('#username');
          var contentNode = document.querySelector('textarea');

          if (usernameNode.value === '' || /^\s*$/.test(usernameNode.value)) {
            alert('昵称不能为空！');
          } else if (contentNode.value === '' || /^\s*$/.test(contentNode.value)) {
            alert('评论内容不能为空！');
          } else {
            formData = new FormData();

            formData.append('username', usernameNode.value);
            formData.append('content', contentNode.value);
            formData.append('artiId', location.search.split('=').slice(-1));

            fetch('/comment', {
              method: 'post',
              body: formData
            }).then(function(resp) {
              return resp.json();
            }).then(function(data) {
              if (data) {
                usernameNode.value = '';
                contentNode.value = '';

                alert('评论成功！');

                location.reload();
              } else {
                alert('评论失败！');
              }
            });
          }
        };
    </script>
</section>
</body>
</html>
